<template>
  <div>
    <div>
      <we-button
        v-if="baseCode"
        @click="dialogFormVisible = true"
        type="primary"
        size="mini"
      >
        <em class="el-icon-plus"></em> 新增
      </we-button>
      <we-button
        v-if="baseCode"
        @click="detailRow(currentRow)"
        type="primary"
        size="mini"
      >
        <em class="el-icon-edit"></em> 修改
      </we-button>
      <we-button v-if="baseCode" @click="deleteRow" type="primary" size="mini">
        <em class="el-icon-delete"></em> 删除
      </we-button>
      <span v-if="baseName">当前人员：【 {{baseName}} 】</span>
    </div>

    <!-- 表格 -->
    <el-table
      size="mini"
      height="116px"
      :header-cell-style="rowClass"
      :data="tableData"

      border
      highlight-current-row
      resizable
      ref="singleTable"
      @row-dblclick="detailRow"
      @current-change="handleCurrentChange"
      style="width: 100%; margin-top: 5px"
    >
      <el-table-column type="index" label="#"> </el-table-column>
      <el-table-column
        prop="politicalOutlook_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="政治面貌"
      >
      </el-table-column>
      <el-table-column
        prop="joinTime"
        :show-overflow-tooltip="true"
        width="150"
        label="参加时间"
      >
      </el-table-column>
      <el-table-column
        prop="workerTime"
        :show-overflow-tooltip="true"
        width="150"
        label="转正时间"
      >
      </el-table-column>
      <el-table-column
        prop="partyJob"
        :show-overflow-tooltip="true"
        width="200"
        label="党派内职务"
      >
      </el-table-column>
      <el-table-column
        prop="joinPartyCompany"
        :show-overflow-tooltip="true"
        width="230"
        label="参加党派时所在单位"
      >
      </el-table-column>
      <el-table-column
        prop="introducer"
        :show-overflow-tooltip="true"
        width="150"
        label="介绍人"
      >
      </el-table-column>
      <el-table-column
        prop="exceptionType_dictText"
        :show-overflow-tooltip="true"
        width="150"
        label="异常类别"
      >
      </el-table-column>
      <el-table-column
        prop="abnormalCauses"
        :show-overflow-tooltip="true"
        width="250"
        label="异常原因"
      >
      </el-table-column>
      <!-- <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <we-button @click="detailRow(scope.row)" type="primary" size="mini"
            >详情</we-button
          >
          <we-button @click="deleteRow(scope.row, i)" type="danger" size="mini">删除</we-button>
        </template>
      </el-table-column> -->
    </el-table>
    <div class="table_tools">
      <we-pagination
        :total="pagination.total"
        :page="pagination.pageNum"
        :pageSize="pagination.pageSize"
        @currentChange="currentChange($event)"
      ></we-pagination>
    </div>
    <!-- 表格 -->

    <!-- 新增弹窗 -->
    <el-dialog
      title="政治面貌及党籍情况子集"
      :visible.sync="dialogFormVisible"
      @close="handleOffDialog"
      append-to-body
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="150px"
        size="mini"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="政治面貌" prop="politicalOutlook">
              <el-select
                class="inputW"
                v-model="ruleForm.politicalOutlook"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in politicalOutlook"
                  :key="item.uuid"
                  :label="item.name"
                  :value="item.uuid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参加时间" prop="joinTime">
              <el-date-picker
                v-model="ruleForm.joinTime"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="转正时间" prop="workerTime">
              <el-date-picker
                v-model="ruleForm.workerTime"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="党派内职务" prop="partyJob">
              <el-input
                class="inputW"
                v-model="ruleForm.partyJob"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="参加党派时所在单位" prop="joinPartyCompany">
              <el-input
                class="inputW"
                v-model="ruleForm.joinPartyCompany"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="介绍人" prop="introducer">
              <el-input
                class="inputW"
                v-model="ruleForm.introducer"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="异常类别" prop="exceptionType">
              <el-select
                class="inputW"
                v-model="ruleForm.exceptionType"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in exceptionCategory"
                  :key="item.uuid"
                  :label="item.name"
                  :value="item.uuid"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="异常原因" prop="abnormalCauses">
              <el-input
                class="inputW"
                v-model="ruleForm.abnormalCauses"
                autocomplete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <we-button size="mini" @click="handleOffDialog"
          >取 消</we-button
        >
        <we-button
          v-if="!isUpdate"
          size="mini"
          type="primary"
          @click="submitForm('ruleForm')"
          >新 增</we-button
        >
        <we-button
          v-if="isUpdate"
          size="mini"
          type="primary"
          @click="updateForm('ruleForm')"
          >修 改</we-button
        >
      </div>
    </el-dialog>
    <!-- 新增弹窗 -->
  </div>
</template>

<script>
import WeButton from "@/components/WeButton";
import WePagination from "@/components/WePagination";
import { getAction, postAction, deleteAction } from "@/api/manage";
import {bus} from "@/utils/eventBus";
import {opDictNew} from "@/api/apiUrl";
export default {
  components: {
    WeButton,
    WePagination,
  },
  data() {
    return {
      pagination: {
        total: 10, // 总数据条数
        pageSize: 10, // 每页条数
        pageNum: 1, // 当前页
      }, // 列表分页数据
      isUpdate: false,
      tableData: [],
      currentRow: null,
      dialogFormVisible: false,
      ruleForm: {
        politicalOutlook: "",
        joinTime: "",
        workerTime: "",
        partyJob: "",
        joinPartyCompany: "",
        introducer: "",
        exceptionType: "",
        abnormalCauses: "",
        uuidList:[],
      },
      rules: {
        politicalOutlook: [
          { required: true, message: "请选择政治面貌", trigger: "change" },
        ],
        joinTime: [
          { required: true, message: "请选择参加时间", trigger: "change" },
        ],
        workerTime: [
          { required: true, message: "请选择转正时间", trigger: "change" },
        ],
        partyJob: [
          { required: true, message: "请填写党派内职务", trigger: "blur" },
        ],
        introducer: [
          { required: true, message: "请填写介绍人", trigger: "blur" },
        ],
      },
      options: [],
      politicalOutlook: [], // 政治面貌
      exceptionCategory: [], // 异常状态
      baseCode: "",
      baseName: "",
    };
  },
  watch: {
    dialogFormVisible() {
      if (this.dialogFormVisible === false) {
        if (this.isUpdate) {
          setTimeout(() => {
            this.isUpdate = false;
            this.clearForm();
            if (this.$refs.ruleForm !== undefined) {
              this.$refs.ruleForm.resetFields();
            }
          }, 500);
        }
        if (this.$store.state.political === true) {
          this.$store.commit("changePolitical", false);
        }
      }
    },
    "$store.state.baseCode"(newval) {
      this.baseCode = newval;
      this.getUserMsg();
    },
    '$store.state.baseName'(newval) {
      this.baseName  = newval
    },
    "$store.state.political"(newval) {
      if (newval === true) {
        this.dialogFormVisible = true;
        if (this.$store.state.updateSubset === true) {
          this.isUpdate = true;
        } else {
          this.isUpdate = false;
        }
        this.$store.commit("changeUpdateSubset", false);
        this.baseCodes = this.$store.state.baseCodes;
        this.getPoliticalOutlook();
        this.getExceptionCategory();
      }
    },
  },
  methods: {
    clickSubset() {
      this.baseCode = this.$store.state.baseCode;
      this.getUserMsg();
      this.getPoliticalOutlook();
      this.getExceptionCategory();
    },
    batchAdd(data){
      this.dialogFormVisible = true
      this.ruleForm.uuidList = data
    },
    /*
     * 新增
     */
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = this.ruleForm;
          data.baseCode = this.baseCode;
          data.sysOrgCode = '测试部门'
          data.createUser = '测试员'
          if (this.$store.state.political === true) {
            for (let i = 0; i < this.baseCodes.length; i++) {
              data.baseCode = this.baseCodes[i];
              postAction("/pmPoliticalOutlook/add", data).then((result) => {
                if (result.code === 200) {
                  this.dialogFormVisible = false;
                  bus.$emit('handleDialog')
                  this.$message({
                    type: "success",
                    message: "新增成功!",
                  });
                  this.clearForm();
                } else {
                  this.$message({
                    type: "waring",
                    message: result.msg,
                  });
                }
              });
            }
          } else {
            postAction("/pmPoliticalOutlook/add", data).then((result) => {
              if (result.code === 200) {
                this.dialogFormVisible = false;
                bus.$emit('handleDialog')
                this.getUserMsg();
                this.$message({
                  type: "success",
                  message: "新增成功!",
                });
              } else {
                this.$message({
                  type: "waring",
                  message: result.msg,
                });
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    /**
     * 修改
     */
    updateForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const data = JSON.parse(JSON.stringify(this.ruleForm));
          postAction("/pmPoliticalOutlook/update", data).then((result) => {
            if (result.code === 200) {
              this.dialogFormVisible = false;
              bus.$emit('handleDialog')
              this.getUserMsg();
              this.$message({
                type: "success",
                message: "修改成功!",
              });
            } else {
              this.$message({
                type: "waring",
                message: result.msg,
              });
            }
          });
        } else {
          return false;
        }
      });
    },
    handleOffDialog() {
      this.dialogFormVisible = false
      bus.$emit('handleDialog')
    },
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    /**
     * 删除
     */
    deleteRow(row, i) {
      this.$confirm("是否要删除这条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteAction("/pmPoliticalOutlook/delete", {
            uuids: [this.currentRow.uuid],
          }).then((result) => {
            if (result.code === 200) {
              this.getUserMsg();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            } else {
              this.$message({
                type: "waring",
                message: result.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    /**
     * 根据uuid查询子集
     */
    getUserMsg() {
      postAction("/pmPoliticalOutlook/pageList", {
        baseCode: this.baseCode,
        size: this.pagination.pageSize,
        page: this.pagination.pageNum,
      }).then((result) => {
        if (result.code === 200) {
          if (!result.data.list.length && this.pagination.pageNum !== 1) {
            this.pagination.pageNum -= 1;
            this.getUserMsg();
            return;
          }
          this.pagination.total = result.data.total;
          this.tableData = result.data.list;
        }
      });
    },

    /**
     * 获取政治面貌字典
     */
    async getPoliticalOutlook() {
      const result = await  getAction(`${opDictNew.findTreeByType}?type=rs_at`)
      this.politicalOutlook = result.data;
    },

    /**
     * 获取异常状态字典
     */
    async getExceptionCategory() {
      const result = await  getAction(`${opDictNew.findTreeByType}?type=rs_ca`)
      this.exceptionCategory = result.data;
    },

    /**
     * 详情
     */
    detailRow(row) {
      if (!row) {
        this.$message("请选择要编辑的数据！");
        return;
      }
      this.ruleForm = JSON.parse(JSON.stringify(row));
      this.isUpdate = true;
      this.dialogFormVisible = true;
    },

    /**
     * 当前页改变
     */
    currentChange(event) {
      this.pagination.pageNum = event;
      this.getUserMsg();
    },

    /**
     * 清除form
     */
    clearForm() {
      this.ruleForm = {
        politicalOutlook: "",
        joinTime: "",
        workerTime: "",
        partyJob: "",
        joinPartyCompany: "",
        introducer: "",
        exceptionType: "",
        abnormalCauses: "",
      };
    },

    handleClose() {},
    rowClass({ row, rowIndex }) {
      return "background:rgba(248, 248, 249, 1);color:#000;";
    },
  },
};
</script>

<style scoped>
.table_tools {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
